<?php
namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\models\Shop;

class ShopController extends  Controller
{
    /**
     * 添加商品数据
     */
    public function actionProduct_addo()
    {
        $request = Yii::$app->request;
        $img = $_FILES['shop_img'];

        if($request->isPost){
            //接收表单传递过来的数据
            $data = $request->post();
            // var_dump($data);die;

            //验证文件是否存在
            if(!empty($img)){
                $destination = 'upload/'.$img['name'];
                move_uploaded_file($img['tmp_name'],$destination);
                $post_data['shop_img'] = $destination;
            }


            $res = Yii::$app->db->createCommand()->insert('shop', [
                'shop_header' => $data['shop_header'],
                'shop_name' => $data['shop_name'],
                'shop_type' => $data['shop_type'],
                'shop_sort' => $data['shop_sort'],
                'shop_speak' => $data['shop_speak'],
                'shop_length' => $data['shop_length'],
                'shop_weight' => $data['shop_weight'],
                'shop_height' => $data['shop_height'],
                'shop_place' => $data['shop_place'],
                'shop_quality' => $data['shop_quality'],
                'shop_supplier' => $data['shop_supplier'],
                'shop_company' => $data['shop_company'],
                'shop_heft' => $data['shop_heft'],
                'shop_showprice' => $data['shop_showprice'],
                'shop_market' => $data['shop_market'],
                'shop_cost' => $data['shop_cost'],
                'shop_minimum' => $data['shop_minimum'],
                'shop_begintime' => $data['shop_begintime'],
                'shop_endtime' => $data['shop_endtime'],
                'shop_keyword' => $data['shop_keyword'],
                'shop_abstract' => $data['shop_abstract'],
                'shop_img' => $post_data['shop_img'],
                'shop_content' => $data['shop_content'],
            ])->execute();

            if($res){
                return $this->redirect('/admin/product_list');
            }else{
                return $this->redirect('/admin/product_add');
            }
        }
    }

    /**
     * 删除数据
     */
    public function actionDelete(){
        $request = Yii::$app->request;
        $id = $request->get('shop_id');
        // var_dump($id);die;
        $res = Yii::$app->db->createCommand()->delete('shop','shop_id=' . $id)->execute();
        // if($res){
        //     return $this->redirect('/admin/product_list');
        // }else{
        //     return $this->redirect('/admin/product_list');
        // }
        return $res;
    }
    /**
     * 修改数据
     */
    public function actionUpdate(){
        $request = Yii::$app->request;
        $id = $request->post('id');
        $res = Yii::$app->db->createCommand()->update('shop','shop_id=' . $id)->execute();
    }

    /**
     * 修改品牌排序
     */
    public function actionProduct_update(){
        $request = Yii::$app->request;
        $id = $request->post('brand_id');
        $sort = $request->post('brand_sort');
        $res = Yii::$app->db->createCommand("UPDATE brand SET brand_sort='$sort' WHERE brand_id=$id")->execute();
        if($res){
            echo json_encode(['code'=>200,'msg'=>'修改成功']);
        }else{
            echo json_encode(['code'=>400,'msg'=>'修改失败']);
        }
    }

    /**
     * 删除品牌
     */
    public function actionProduct_del(){
        $request = Yii::$app->request;
        $id = $request->post('brand_id');
        $res = Yii::$app->db->createCommand()->delete('brand','brand_id = ' . $id)->execute();
        if($res){
            echo json_encode(['code'=>200,'msg'=>'删除成功']);
        }else{
            echo json_encode(['code'=>400,'msg'=>'删除失败']);
        }
    }

    public function actionAdd_brand(){
        $data = Yii::$app->request->post();
        $files = $_FILES['files'];
        if(!empty($files)){
            $destination = 'upload/'.$files['name'];
            move_uploaded_file($files['tmp_name'],$destination);
            $img_file = $destination;
        }
        $res = Yii::$app->db->createCommand()->insert('logos', [
            'type_name' => $data['type_name'],
            'brandclass' => $data['brandclass'],
            'shop_mis' => $data['shop_mis'],
            'img_file' => $img_file

        ])->execute();
        if($res){
            return $this->redirect('/admin/product_brand');
        }
    }

    public function actionDelete_logos(){
        $id = $_POST['id'];
        $res = Yii::$app->db->createCommand()->delete('logos','id = ' . $id)->execute();

        if($res){
            return json_encode(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json_encode(['code'=>500,'msg'=>'删除失败']);
        }
    }

}